package com.exchange.used.web.api;


import com.exchange.used.config.converters.LayuiResult;
import com.exchange.used.entity.Role;
import com.exchange.used.entity.User;
import com.exchange.used.service.UserServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.*;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/system")
public class SystemController {

    @Autowired
    UserServiceI userServiceI;
    @Autowired
    private EntityManager entityManager;


    @ResponseBody
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public LayuiResult register(@RequestBody User user) {
        return userServiceI.register(user);
    }

    @ResponseBody
    @RequestMapping(value = "/doLogin/{username}/{password}", method = RequestMethod.GET)
    public LayuiResult doLogin(@PathVariable String username,
                               @PathVariable String password,
                               HttpServletRequest request) {
        LayuiResult result = userServiceI.doLogin(username, password);
        if (result.getCode() == 0) {
            request.getSession().setAttribute("user", result.getData().get("user"));
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/getLoginUserInfo", method = RequestMethod.GET)
    public User doLogin(HttpServletRequest request) {
        return (User) request.getSession().getAttribute("user");
    }

    @ResponseBody
    @RequestMapping(value = "/changeUserInfo", method = RequestMethod.POST)
    public LayuiResult changeUserInfo(@RequestBody User user, HttpServletRequest request) {
        LayuiResult result = userServiceI.changeUserInfo(user);
        if (result.getCode() == 0) {
            request.getSession().setAttribute("user", result.getData().get("user"));
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/changeUserPassword/{userid}/{oldPassword}/{newPassword}/{repeatPassword}", method = RequestMethod.POST)
    public LayuiResult changeUserPassword(@PathVariable String userid,
                                          @PathVariable String oldPassword,
                                          @PathVariable String newPassword,
                                          @PathVariable String repeatPassword,
                                          HttpServletRequest request) {
        LayuiResult result = userServiceI.changePassword(userid, oldPassword, newPassword, repeatPassword);
        if (result.getCode() == 0) {
            request.getSession().setAttribute("user", result.getData().get("user"));
        }
        return result;
    }


    @RequestMapping(value = "/doLogout", method = RequestMethod.GET)
    public String doLogout(HttpServletRequest request) {
        request.getSession().removeAttribute("user");
        return "login";
    }


    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test(HttpServletRequest request) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<User> query = criteriaBuilder.createQuery(User.class);

        Root<User> userRoot = query.from(User.class);
        Join<User, Role> roleOfUser = userRoot.join("fkroleid", JoinType.LEFT);

        List<Predicate> predicatesList = new ArrayList<>();
        predicatesList.add(
                criteriaBuilder.and(
                        criteriaBuilder.like(
                                userRoot.get("username"), "%admin%"
                        ), criteriaBuilder.equal(
                                roleOfUser.get("fkroleid"), "1"
                        )
                )
        );


        query.select(userRoot).where(predicatesList.toArray(new Predicate[predicatesList.size()]));

        TypedQuery<User> typedQuery = entityManager.createQuery(query);
        List<User> resultList = typedQuery.getResultList();
        return "login";
    }


}
